use redis_protocol::resp2::prelude::Frame;
use crate::prelude::*;
use std::sync::Arc;
pub struct Set();
pub struct Get();
pub struct Del();
impl CommandService for Set {
    fn process(&self, s: &Arc<dyn StorageService>, param: Frame) -> Option<Frame>
    {
        let mut p = FrameParser::new(param).unwrap();
        p.next_string().unwrap();
        let key = p.next_string().unwrap();
        let value = p.next_string().unwrap();

        let k = Key::STRING(key);
        let v = Value::STRING(value);

        s.insert(k, v).unwrap();
        Some(FrameConstructor::ok())
    }
}

impl CommandService for Get {
    fn process(&self, s: &Arc<dyn StorageService>, param: Frame) -> Option<Frame>
    {
        let mut p = FrameParser::new(param).unwrap();
        p.next_string().unwrap();
        let key = p.next_string().unwrap();

        let v = s.lookup(Key::STRING(key)).unwrap();
        
        match v {
            Value::STRING(x) => Some(Frame::SimpleString(x)),
            _ => Some(FrameConstructor::err())
        }
    }
}
impl CommandService for Del {
    fn process(&self, s: &Arc<dyn StorageService>, param: Frame) -> Option<Frame>
    {
        let mut p = FrameParser::new(param).unwrap();
        p.next_string().unwrap();
        let key = p.next_string().unwrap();

        s.delete(Key::STRING(key)).unwrap();
        
        Some(FrameConstructor::ok())
    }
}